/**
 * @(#)StructureDao.java, 2008-4-30.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie.persistence.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import edu.pku.sei.pgie.beans.Structure;

/**
 * @author HeLi
 */
public class StructureDAO extends DAOStub {
    DataSource dataSource;

    public StructureDAO() {
        dataSource = DataSourceFactory.getDataSource();
    }

    public Structure createPackage(Structure pkg) throws SQLException {
        if (pkg == null) {
            return null;
        }
        QueryRunner runner = new QueryRunner(dataSource);
        Long id = generateUID();
        List children = pkg.getChildren();
        if (children == null)
            children = new LinkedList();
        runner.update("insert into STRUCTURE(ID,NAME,PARENT)\n"
                + "values(?,?,?)", new Object[] {
            id, pkg.getName(), null
        });
        Iterator iterator = children.iterator();
        while (iterator.hasNext()) {
            Structure child = (Structure) iterator.next();
            Long uid = generateUID();
            runner.update(
                    "insert into STRUCTURE(id,name,parent) values(?,?,?)",
                    new Object[] {
                        uid, child.getName(), id
                    });
            child.setId(id);
            child.setChildren(null);
        }
        pkg.setId(id);
        return pkg;
    }

    public List loadPackages() throws SQLException {
        QueryRunner runner = new QueryRunner(dataSource);
        final List result = new LinkedList();
        runner.query("select * from STRUCTURE where PARENT is NULL",
                new ResultSetHandler() {
                    public Object handle(ResultSet resultSet)
                            throws SQLException {
                        Structure structure = new Structure();
                        structure.setId(new Long(resultSet.getLong("id")));
                        structure.setName(resultSet.getString("name"));
                        structure.setChildren(null);
                        result.add(structure);
                        return structure;
                    }
                });
        return result;
    }

    public Structure getChildren(Structure structure) throws SQLException {
        if (structure == null)
            return null;
        QueryRunner runner = new QueryRunner(dataSource);
        final List result = new LinkedList();
        runner.query("select * from STRUCTURE where PARENT = ?", structure
                .getId(), new ResultSetHandler() {
            public Object handle(ResultSet resultSet) throws SQLException {
                Structure structure = new Structure();
                structure.setId(new Long(resultSet.getLong("id")));
                structure.setName(resultSet.getString("name"));
                structure.setChildren(null);
                result.add(structure);
                return structure;
            }
        });
        structure.setChildren(result);
        return structure;
    }
}
